<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="物料编码" prop="invCode">
        <el-input
          v-model="queryParams.invCode"
          placeholder="请输入物料编码"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="invName">
        <el-input
          v-model="queryParams.invName"
          placeholder="请输入物料名称"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="物料类型" prop="materialType">
        <el-select
          v-model="queryParams.materialType"
          placeholder="请选择物料类型"
          style="width: 200px"
        >
          <el-option
            v-for="dict in materialTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="规格型号" prop="invStd">
        <el-input
          v-model="queryParams.invStd"
          placeholder="请输入规格型号"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="物料分组" prop="materialGroup">
        <el-select
          v-model="queryParams.materialGroup"
          placeholder="请选择物料分组"
          style="width: 200px"
        >
          <el-option
            v-for="dict in materialGroupOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="是否追述" prop="isTracing">
        <el-select
          v-model="queryParams.isTracing"
          placeholder="请选择是否追述"
          clearable
          size="small"
          style="width: 200px"
        >
          <el-option
            v-for="dict in isTracingOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="供应商" prop="supplierId">
        <el-select
          v-model="queryParams.supplierId"
          filterable
          placeholder="请选择供应商"
          style="width: 200px"
        >
          <el-option
            v-for="item in supplierIdOptions"
            :key="item.supplierId"
            :label="item.supplierName"
            :value="item.supplierId"
          >
            <span style="float: left">{{ item.supplierCode }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.supplierName }}</span>
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:material:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:material:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:material:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:material:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="materialList"
      @selection-change="handleSelectionChange"
      style="width: 100%"
    >
      <el-table-column type="selection" width="60" align="center" fixed="left" />
      <el-table-column label="物料主键id" align="center" prop="dataId" width="160" fixed="left" v-if="false" />
      <el-table-column label="物料编码" align="center" prop="invCode" width="120" fixed="left" />
      <el-table-column label="旧物料编码" align="center" prop="invCodeOld" width="120" fixed="left" />
      <el-table-column label="物料名称" align="center" prop="invName" width="200" />
      <!-- <el-table-column label="物料类型" align="center" prop="materialType" :formatter="materialTypeFormat" width="100" /> -->
      <el-table-column label="规格型号" align="center" prop="invStd" width="120" />
      <el-table-column label="大类编码" align="center" prop="invCCode" width="120" />
      <el-table-column label="大类名称" align="center" prop="invCName" width="120" />
      <el-table-column label="计量单位" align="center" prop="unitCode" width="120" />
      <!-- <el-table-column label="物料分组" align="center" prop="materialGroup" :formatter="materialGroupFormat" width="100" /> -->
      <el-table-column label="物料管理模式" align="center" prop="bInwBatch" :formatter="(row) => isOnlyType(row)" width="160" />
      <el-table-column label="是否成套件" align="center" prop="isAccessary" :formatter="(row) => isAllType(row, 'isAccessary')" width="160" />
      <el-table-column label="是否生产耗用" align="center" prop="isConsume" :formatter="(row) => isAllType(row, 'isConsume')" width="160" />
      <!-- <el-table-column label="是否追述" align="center" prop="isTracing" :formatter="isTracingFormat" width="100" /> -->
      <!-- <el-table-column label="供应商" align="center" prop="supplierId" :formatter="supplierIdFormat" width="200" /> -->
      <el-table-column label="是否在制" align="center" prop="isProducing" :formatter="(row) => isAllType(row, 'isProducing')" width="120" />
      <el-table-column label="是否外购" align="center" prop="isPurchase" :formatter="(row) => isAllType(row, 'isPurchase')" width="120" />
      <el-table-column label="是否销售" align="center" prop="isSale" :formatter="(row) => isAllType(row, 'isSale')" width="120" />
      <el-table-column label="是否自制" align="center" prop="isSelf" :formatter="(row) => isAllType(row, 'isSelf')" width="120" />
      <!-- <el-table-column label="是否删除" align="center" prop="isDelete" :formatter="(row) => isAllType(row, 'isDelete')" width="120" /> -->
      <el-table-column label="启用日期" align="center" prop="dsDate" width="160" />
      <el-table-column label="停用日期" align="center" prop="deDate" width="160" />
      <el-table-column label="创建时间" align="center" prop="createDate" width="160" />
      <el-table-column label="创建人" align="center" prop="createUser" width="160" />
      <el-table-column label="修改日期" align="center" prop="updateDate" width="160" />
      <el-table-column label="修改人" align="center" prop="updateUser" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            style="margin-right: 9px"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:material:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:material:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="760px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row :gutter="20">
          <el-col :lg="12">
            <el-form-item label="物料编码" prop="invCode">
              <el-input
                v-model="form.invCode"
                placeholder="请输入物料编码"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="物料名称" prop="invName">
              <el-input
                v-model="form.invName"
                placeholder="请输入物料名称"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="规格型号" prop="invStd">
              <el-select
                v-model="form.invStd"
                placeholder="请选择规格型号"
                style="width: 240px"
              >
                <el-option
                  v-for="dict in materialTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :lg="12">
            <el-form-item label="物料分组" prop="materialGroup">
              <el-select
                v-model="form.materialGroup"
                placeholder="请选择物料分组"
                style="width: 240px"
              >
                <el-option
                  v-for="dict in materialGroupOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :lg="12">
            <el-form-item label="计量单位" prop="unitCode">
              <el-input
                v-model="form.unitCode"
                placeholder="请输入计量单位"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <!-- <el-form-item label="物料特征值" prop="materialFace">
              <el-input
                v-model="form.materialFace"
                placeholder="请输入物料特征值"
                style="width: 240px"
              />
            </el-form-item>
          </el-col> -->
          <el-col :lg="12">
            <el-form-item label="是否追述" prop="isTracing">
              <el-select v-model="form.isTracing" placeholder="请选择是否追述" style="width: 240px">
                <el-option
                  v-for="dict in isTracingOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :lg="12">
            <el-form-item label="供应商" prop="supplierId">
              <el-select
                v-model="form.supplierId"
                filterable
                placeholder="请选择供应商"
                style="width: 240px"
              >
                <el-option
                  v-for="item in supplierIdOptions"
                  :key="item.supplierId"
                  :label="item.supplierName"
                  :value="item.supplierId"
                >
                  <span style="float: left">{{ item.supplierCode }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.supplierName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <!-- <el-col :lg="12">
            <el-form-item label="总成类型" prop="assemblyType">
              <el-select
                v-model="form.assemblyType"
                filterable
                placeholder="请选择总成类型"
                style="width: 240px"
              >
                <el-option
                  v-for="item in assemblyTypeOptions"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                >
                  <span style="float: left">{{ item.dictValue }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.dictLabel }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <!-- <el-col :lg="12">
            <el-form-item label="有效时间" prop="validTime">
              <el-input
                type="number"
                v-model="form.validTime"
                placeholder="请输入有效时间"
                style="width: 240px"
              />
            </el-form-item>
          </el-col> -->
          <!-- <el-col :lg="12">
            <el-form-item label="物料描述" prop="materialDescribe">
              <el-input
                v-model="form.materialDescribe"
                placeholder="请输入物料描述"
                type="textarea"
                :rows="4"
                style="width: 240px"
              />
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMaterial,
  getMaterial,
  delMaterial,
  addMaterial,
  updateMaterial,
  exportMaterial,
} from "@/api/base/materialERP";
import { listAllSupplier } from "@/api/base/supplier";

export default {
  name: "Material",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料信息表格数据
      materialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 物料类型字典
      materialTypeOptions: [],
      // 物料分组字典
      materialGroupOptions: [],
      // 是否追述字典
      isTracingOptions: [],
      // 供应商字典
      supplierIdOptions: [],
      // 总成类型字典
      assemblyTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        invCode: undefined,
        invName: undefined,
        invStd: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        invCode: [ { required: true, message: "物料编码不能为空", trigger: "blur" } ],
        invName: [ { required: true, message: "物料名称不能为空", trigger: "blur" } ],
        invStd: [ { required: true, message: "规格型号不能为空", trigger: "blur" } ]
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("mes_material_type").then((response) => {
      this.materialTypeOptions = response.data;
    });
    this.getDicts("mes_material_team").then((response) => {
      this.materialGroupOptions = response.data;
    });
    this.getDicts("sys_yes_no").then((response) => {
      this.isTracingOptions = response.data;
    });
    this.getDicts("assembly_type").then((response) => {
      this.assemblyTypeOptions = response.data;
    });
    listAllSupplier().then((response) => {
      this.supplierIdOptions = response;
    });
  },
  methods: {
    /** 查询物料信息列表 */
    getList() {
      this.loading = true;
      listMaterial(this.queryParams).then((response) => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 物料类型字典翻译
    materialTypeFormat(row, column) {
      return this.selectDictLabel(this.materialTypeOptions, row.materialType);
    },
    // 物料分组字典翻译
    materialGroupFormat(row, column) {
      return this.selectDictLabel(this.materialGroupOptions, row.materialGroup);
    },
    // 是否追述字典翻译
    isTracingFormat(row, column) {
      return this.selectDictLabel(this.isTracingOptions, row.isTracing);
    },
    // 供应商字典翻译
    supplierIdFormat(row, column) {
      var actions = [];
      Object.keys(this.supplierIdOptions).some((key) => {
        if (this.supplierIdOptions[key].supplierId == "" + row.supplierId) {
          actions.push(this.supplierIdOptions[key].supplierName);
          return true;
        }
      });
      return actions.join("");
    },
    // 个别翻译
    isOnlyType(row) {
      let str = '';
      switch(row.bInwBatch) {
        case '1':
          str = 'PSN'
          break;
        case '2':
          str = 'SN'
          break;
        default: console.log('no default')
      }
      return str;
    },
    // 批量翻译
    isAllType(row, name) {
      let str = '';
      switch(row[name]) {
        case '0':
          str = '否'
          break;
        case '1':
          str = '是'
          break;
        default: console.log('no default')
      }
      return str;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.dataId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const materialId = row.dataId || this.ids;
      getMaterial(materialId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.dataId != null) {
            updateMaterial(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addMaterial(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const materialIds = row.dataId || this.ids;
      this.$confirm(
        '是否确认删除物料信息编号为"' + materialIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delMaterial(materialIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有物料信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportMaterial(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        })
        .catch(function () {});
    },
  },
};
</script>
